﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Fluent;
using OpenPowerPoint.Slides;

namespace OpenPresenter
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        Backend _backend;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void RibbonWindow_Loaded(object sender, RoutedEventArgs e)
        {
            _backend = new Backend();

            CommandBindings.Add(new CommandBinding(Backend.OpenFileCommand, OpenFile_Executed, _backend.OpenFile_CanExecute));

            DataContext = _backend;
        }

        private void OpenFile_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            _backend.OpenFile_Executed(sender, e);

            backstage.IsOpen = false;
        }

        private void Grid_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue is SlideGroup)
                return;

            //We have to remove all bindings in the VisualTree before applying them to their new parents
            Slide context = e.NewValue as Slide;

            foreach (var shape in context.Shapes)
            {
                if (shape.Content.Parent is FlowDocumentScrollViewer)
                {
                    ((FlowDocumentScrollViewer)shape.Content.Parent).Document = null;
                }
            }
        }
    }
}
